package com.glory.dmegcpda.view

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.databinding.DataBindingUtil
import com.glory.dmegcpda.R
import com.glory.dmegcpda.databinding.ActivityQualityCheckBinding
import com.glory.dmegcpda.utils.PopWindowsUtils
import com.glory.dmegcpda.viewmodel.QualityCheckViewModel
import com.glory.mvvmnet.base.BaseActivity
import com.glory.mvvmnet.entity.ToastMessageBean
import kotlinx.android.synthetic.main.activity_quality_check.*
import kotlinx.android.synthetic.main.activity_quality_check.btnConfirm
import kotlinx.android.synthetic.main.activity_quality_check.btnSubmit

/**
 * FileName: QualityCheckActivity
 * Author: XJH
 * Date: 2021/10/13 14:13
 * Description:
 * History:
 */
class QualityCheckActivity : BaseActivity<QualityCheckViewModel>(), View.OnClickListener {
    var mBinding : ActivityQualityCheckBinding? = null
    private var defectTypePop : PopWindowsUtils.GeneralPop? = null
    private var defectReasonPop : PopWindowsUtils.GeneralPop? = null
    private var holdCodePop : PopWindowsUtils.GeneralPop? = null
    private var releaseCodePop : PopWindowsUtils.GeneralPop? = null
    private var holdReasonPop : PopWindowsUtils.GeneralPop? = null
    private var submitType = "defect"

    override fun initData() {
        viewModel.defUI.callObserve.observe(this){
            when(it.type){
                101 ->{
                    rvHoldList.adapter = viewModel.mAdapter
                    viewModel.getLevel()
                    mBinding!!.qualityCheckViewModel = viewModel
                }
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        mBinding = DataBindingUtil.setContentView(this,R.layout.activity_quality_check)
        mBinding!!.qualityCheckViewModel = viewModel
        etAssembly.requestFocus()
        viewModel.init()
        flDefectType.setOnClickListener(this)
        flDefectReason.setOnClickListener(this)
        flHoldCode.setOnClickListener(this)
        flHoldReason.setOnClickListener(this)
        flReleaseCode.setOnClickListener(this)
        btnConfirm.setOnClickListener(this)
        btnHold.setOnClickListener(this)
        btnSubmit.setOnClickListener(this)
        ivBack.setOnClickListener(this)
        rbDefect.setOnClickListener(this)
        rbHold.setOnClickListener(this)
        rbRelease.setOnClickListener(this)
        etAssembly.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(text: Editable?) {
                val toString = text.toString()
                if (toString.indexOf("\r") >= 0 || toString.indexOf("\n") >= 0) {
                    etAssembly.setText(toString.replace("\r", "").replace("\n", ""))
                    defectTypePop = null
                    defectReasonPop = null
                    holdCodePop = null
                    holdReasonPop = null
                    tvHoldCode.text = ""
                    tvHoldReason.text = ""
                    tvDefectReason.text = ""
                    tvDefectType.text = ""
                    viewModel.getLotInfo(etAssembly.text.toString().trim())
                }
            }

        })
    }

    override fun layoutId(): Int {
        return R.layout.activity_quality_check
    }

    override fun onClick(p: View?) {
        when(p?.id){
            R.id.flDefectType ->{
                if (viewModel.defectTypeList.size <= 0){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("未获取到不良类别",false)
                    return
                }
                viewModel.defectReasonList.clear()
                if (defectTypePop == null){
                    defectTypePop = PopWindowsUtils.getOneChoosePop(this, flDefectType.width, viewModel.defectTypeList, ivDefectType,object : PopWindowsUtils.PopTextAndPositionCallback{
                        override fun onSuccess(codeS: String, potion: Int) {
                            tvDefectType.text = codeS
                            viewModel.getDefectReason(codeS)
                        }

                    })

                }

                if (defectTypePop!!.isShowing) {
                    defectTypePop?.dismiss()
                } else {
                    PopWindowsUtils.setArrowShow(ivDefectType)
                    defectTypePop?.showAsDropDown(flDefectType, 0, 0)
                }
            }
            R.id.flDefectReason ->{
                if (viewModel.defectReasonList.size <= 0){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("未获取到不良原因，请先选择不良类别",false)
                    return
                }

                if (defectReasonPop == null){
                    defectReasonPop = PopWindowsUtils.getOneChoosePop(this, flDefectReason.width, viewModel.defectReasonList, ivDefectReason,object : PopWindowsUtils.PopTextAndPositionCallback{
                        override fun onSuccess(codeS: String, potion: Int) {
                            tvDefectReason.text = codeS
                        }

                    })

                }

                if (defectReasonPop!!.isShowing) {
                    defectReasonPop?.dismiss()
                } else {
                    PopWindowsUtils.setArrowShow(ivDefectReason)
                    defectReasonPop?.showAsDropDown(flDefectReason, 0, 0)
                }
            }
            R.id.flHoldCode ->{
                if (viewModel.holdCodeList.size <= 0){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("未获取到暂停码",false)
                    return
                }

                if (holdCodePop == null){
                    holdCodePop = PopWindowsUtils.getOneChoosePop(this, flHoldCode.width, viewModel.holdCodeList, ivHoldCode,object : PopWindowsUtils.PopTextAndPositionCallback{
                        override fun onSuccess(codeS: String, potion: Int) {
                            tvHoldCode.text = codeS.split("##")[0]
                            tvHoldReason.text = codeS.split("##")[1]
                        }

                    })

                }

                if (holdCodePop!!.isShowing) {
                    holdCodePop?.dismiss()
                } else {
                    PopWindowsUtils.setArrowShow(ivHoldCode)
                    holdCodePop?.showAsDropDown(flHoldCode, 0, 0)
                }
            }
            R.id.flHoldReason ->{

            }
            R.id.flReleaseCode ->{
                if (viewModel.releaseCodeList.size <= 0){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("未获取到放行码",false)
                    return
                }

                if (releaseCodePop == null){
                    releaseCodePop = PopWindowsUtils.getOneChoosePop(this, flReleaseCode.width, viewModel.releaseCodeList, ivReleaseCode,object : PopWindowsUtils.PopTextAndPositionCallback{
                        override fun onSuccess(codeS: String, potion: Int) {
                            tvReleaseCode.text = codeS.split("##")[0]
                            tvReleaseReason.text = codeS.split("##")[1]
                        }

                    })

                }

                if (releaseCodePop!!.isShowing) {
                    releaseCodePop?.dismiss()
                } else {
                    PopWindowsUtils.setArrowShow(ivReleaseCode)
                    releaseCodePop?.showAsDropDown(flReleaseCode, 0, 0)
                }
            }
            R.id.btnConfirm ->{
                if (etAssembly.text.toString().isEmpty()){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("请先扫描组件号",false)
                    return
                }

                if (tvDefectType.text.toString().isEmpty() || tvDefectReason.text.toString().isEmpty()){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("请先选择不良类别或不良原因",false)
                    return
                }
                viewModel.lotJudge(etAssembly.text.toString(),tvDefectType.text.toString().split("##")[0],tvDefectReason.text.toString().split("##")[0],etDefectCommit.text.toString().trim())
            }
            R.id.btnHold ->{
                if (etAssembly.text.toString().isEmpty()){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("请先扫描组件号",false)
                    return
                }

                if (tvHoldCode.text.toString().isEmpty() || tvHoldReason.text.toString().isEmpty()){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("请先选择暂停码或暂停原因",false)
                    return
                }
                viewModel.hold(etAssembly.text.toString(),tvHoldCode.text.toString(),tvHoldReason.text.toString(),"HOLD",etHoldCommit.text.toString().trim())
            }
            R.id.btnSubmit ->{
                if (etAssembly.text.toString().isEmpty()){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("请先扫描组件号",false)
                    return
                }
                when(submitType){
                    "defect" ->{
                        if (tvDefectType.text.toString().isEmpty() || tvDefectReason.text.toString().isEmpty()){
                            viewModel.defUI.toastEvent.value = ToastMessageBean("请先选择不良类别或不良原因",false)
                            return
                        }
                        viewModel.lotJudge(etAssembly.text.toString(),tvDefectType.text.toString().split("##")[0],tvDefectReason.text.toString().split("##")[0],etDefectCommit.text.toString().trim())
                    }
                    "hold" ->{
                        if (tvHoldCode.text.toString().isEmpty()){
                            viewModel.defUI.toastEvent.value = ToastMessageBean("请先选择暂停码",false)
                            return
                        }
                        viewModel.hold(etAssembly.text.toString(),tvHoldCode.text.toString(),tvHoldReason.text.toString(),"HOLD",etHoldCommit.text.toString().trim())
                    }
                    "release" ->{
                        if (tvReleaseCode.text.toString().isEmpty()){
                            viewModel.defUI.toastEvent.value = ToastMessageBean("请先选择放行码",false)
                            return
                        }

                        viewModel.hold(etAssembly.text.toString(),tvReleaseCode.text.toString(),tvReleaseReason.text.toString(),"RELEASE",etReleaseCommit.text.toString().trim())
                    }
                }
            }
            R.id.ivBack ->{
                finish()
            }
            R.id.rbDefect ->{
                submitType = "defect"
            }
            R.id.rbHold ->{
                submitType = "hold"
            }
            R.id.rbRelease -> {
                submitType = "release"
            }
        }
    }
}